<?php
namespace plugins\sign_in\controller; //插件英文名，改成你的插件英文就行了
use cmf\controller\PluginBaseController;
use think\Request;
use think\Db;
class IndexController extends PluginBaseController
{
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $userId = cmf_get_current_user_id();//获取用户id，可判断是否登录
        if (empty($userId)) {
            $this->error('用户未登录','/');
        }
    }

    //发起请求
    public function index(){
        $user_id=cmf_get_current_user_id();
        $users=db("user")->where("id",$user_id)->find();
        $sign_time=$users['sign_time'];//最后签到时间
        $int=date('Y-m-d');
        $int=strtotime($int);//当前时间
        $ints=$int+86400;  //下一天
        $int_s=$int-86400;  //前一天
        //当天已签到
        $is_sign=0;
        if($int<$sign_time&&$sign_time<$ints){
            $is_sign=1;
        }
        $this->assign("is_sign",$is_sign);
        $this->assign("today_score",$users["sign_count"]);
        //我的签到列表
        $list = db('user_score_log')->where("user_id = $user_id and action='sign'")->order('create_time desc')->paginate(10);
        $pa=$this->request->param("page");
        $this->assign('pa', $pa);
        // 把分页数据赋值给模板变量list
        $this->assign('list', $list);
        $this->assign('page', $list->render());//单独提取分页出来
        //统计当月签到次数
        $start = date('Y-m-01 00:00:00');
        $end = date('Y-m-d H:i:s');
        $month_count=db('user_score_log')->where("create_time>=unix_timestamp('".$start."') and create_time <=unix_timestamp('".$end."') and user_id = $user_id and action='sign'")->count("id");
        $this->assign('month_count', $month_count);
        //总共签到次数
        $year_count=db('user_score_log')->where("user_id = $user_id and action='sign'")->count("id");
        $this->assign('year_count', $year_count);
        //签到得到积分
        $all_count=db('user_score_log')->where("user_id = $user_id and action='sign'")->sum("score");
        if(empty($all_count))
        {
            $all_count=0;
        }
        $this->assign('score', $all_count);
        return $this->fetch("/index");
    }
    //签到post
    public function signpost()
    {
        $user_id=cmf_get_current_user_id();
        $users=db("user")->where("id",$user_id)->find();
        if($users['sign_time'])
        {
            $sign_time=$users['sign_time'];//最后签到时间
            $int=date('Y-m-d');
            $int=strtotime($int);//当前时间
            $ints=$int+86400;  //下一天
            $int_s=$int-86400;  //前一天
            //当天已签到
            if($int<$sign_time&&$sign_time<$ints){
                echo json_encode(array('success'=>0,'msg'=>"已签到",'sign_date'=>$users['sign_count']));die;
            }
            //昨天未签到，积分，天数在签到修改为1
            if($sign_time<$int_s){
                $userdata=[
                    "score"=>$users["score"]+1,
                    "sign_count"=>1,
                    "sign_time"=>time()
                ];
                db("user")->where("id",$user_id)->update($userdata);//更新用户签到
                $sign_insert=[
                    "user_id"=>$user_id,
                    "create_time"=>time(),
                    "action"=>"sign",
                    "score"=>1,
                    "coin"=>1
                ];
                $inid=db("user_score_log")->insert($sign_insert);
                if($inid)
                {
                    echo json_encode(array('success'=>1,'msg'=>"签到成功"));die;
                }
                else
                {
                    echo json_encode(array('success'=>0,'msg'=>"签到失败"));die;
                }
            }
            //连续签到
            if($int_s<$sign_time&&$sign_time<$int){
                $count=$users['sign_count']+1;
                $score=$users['score']+$count;//签到第几天，增加多少分
                $sign_time=time();
                $userdata=[
                    "score"=>$score,
                    "sign_count"=>$count,
                    "sign_time"=>$sign_time
                ];
                db("user")->where("id",$user_id)->update($userdata);//更新用户签到
                $sign_insert=[
                    "user_id"=>$user_id,
                    "create_time"=>$sign_time,
                    "action"=>"sign",
                    "score"=>$count,
                    "coin"=>1
                ];
                $inid=db("user_score_log")->insert($sign_insert);
                if($inid)
                {
                    echo json_encode(array('success'=>1,'msg'=>"签到成功"));die;
                }
                else
                {
                    echo json_encode(array('success'=>0,'msg'=>"签到失败"));die;
                }
            }
        }
        else
        {
            //用户第一次签到
            $userdata=[
                "score"=>$users["score"]+1,
                "sign_count"=>1,
                "sign_time"=>time()
            ];
            db("user")->where("id",$user_id)->update($userdata);//更新用户签到
            $sign_insert=[
                "user_id"=>$user_id,
                "create_time"=>time(),
                "action"=>"sign",
                "score"=>1,
                "coin"=>1
            ];
            $inid=db("user_score_log")->insert($sign_insert);
            if($inid)
            {
                echo json_encode(array('success'=>1,'msg'=>"签到成功"));die;
            }
            else
            {
                echo json_encode(array('success'=>0,'msg'=>"签到失败"));die;
            }
        }

    }
    //获取当月已签到的日期
    public function getsign()
    {
        $user_id=cmf_get_current_user_id();
        $time  = time();

        $year  = date('Y',$time);

        $month = date('m',$time);

        $day  = date("t",strtotime("$year-$month"));

        $start_stime  = strtotime("$year-$month-1 0:0:0")-1;

        $end_stime = strtotime("$year-$month-$day 23:59:59")+1;

        $list = db('user_score_log')->where("user_id = $user_id and action='sign' and create_time > $start_stime and create_time < $end_stime")->order('create_time asc')->select();
        foreach ($list as $key => $value){

            $list[$key] = date('j',$value["create_time"]);

        }

        echo json_encode(array('success'=>1,'date_list'=>$list));die;

    }

}
